package paginas;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import webservice.Empresa;
import webservice.IWebServiceCentral;
import webservice.IWebServiceCentralService;
import webservice.ReservaIndependiente;
import webservice.Usuario;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.FieldEvents.BlurEvent;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.server.VaadinSession;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component.Event;
import com.vaadin.ui.Window.CloseEvent;
import com.vaadin.ui.Window.CloseListener;
import com.vaadin.ui.Window;

public class ActualizarEstadoReserva {
	
	//private OptionGroup tipoReserva = new OptionGroup("Tipo de Rerserva");
	private OptionGroup estados = new OptionGroup("Estados");
	private Table reservasInd = new Table("Reservas Independientes");
	//private Table reservasFijas = new Table("Reservas Fijas");
	private Button btnAcepEst = new Button("Aceptar");
	private Button btnSalir = new Button("Salir");
	private Label lblAlta = new Label("Actualizar estado de las Reservas");
	private Panel panel = new Panel();
	private ReservaIndependiente resSeleccionada = new ReservaIndependiente();
	private Window sub = new Window();
	
	VerticalLayout vl = new VerticalLayout();
	protected static final Object[] RESERVA_VISIBLE_COLUMNS = new Object[] {
		"idReserva","fecha","hora","estado","cancha.idCancha"};
	protected static final String[] RESERVA_COLUMN_HEADERS = new String[] {
		"IdReserva", "Fecha","Hora","Estado", "Cancha"};
	
	public Panel panelActualizarEstadoReserva(){
		
		
		//tipoReserva.addItem("Independientes");
		//tipoReserva.addItem("Fijas");
		//tipoReserva.addStyleName("vertical");
		//tipoReserva.setSizeUndefined();
		//tipoReserva.setImmediate(true);
		
		
		
		
				
		
		lblAlta.setStyleName("tituloP");
		vl.setMargin(true);
	//	vl.setSizeFull();
		vl.setStyleName("vLayout");
	//	vl.addComponent(tipoReserva);
		
		
	/*	tipoReserva.addValueChangeListener(new ValueChangeListener() {
			
			
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				if(event.getProperty().getValue().equals("Independientes"))
					panelIndependientes();
				else if(event.getProperty().getValue().equals("Fijas"))
					panelFijas();
				
			}
		});*/
		//vl.addComponent(panelR);
		panel.setContent(vl);
		panelIndependientes();
		
		btnAcepEst.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				
				sub.close();
				IWebServiceCentralService wss = new IWebServiceCentralService();
				IWebServiceCentral ws = wss.getIWebServiceCentralPort();
				int retorno = 0;
				String estado = estados.getValue().toString();
				retorno = ws.actualizarEstadoReservaIndependiente(resSeleccionada, estado);
				if (retorno == 1) {
					Window sub2 = new Window();
					VerticalLayout vLayout = new VerticalLayout();
					vLayout.setMargin(true);

					Label lblOk = new Label("Estado cambiado");
					lblOk.setSizeUndefined();
					vLayout.addComponent(lblOk);
					vLayout.setComponentAlignment(lblOk,
							Alignment.MIDDLE_CENTER);
					sub.setContent(vLayout);
					sub.setWidth("300px");
					sub.setHeight("50px");
					sub.setModal(true);
					UI.getCurrent().addWindow(sub);

					sub.addCloseListener(new CloseListener() {

						/**
						 * 
						 */
						private static final long serialVersionUID = 1L;

						@Override
						public void windowClose(CloseEvent e) {
							((PaginaPrincipal) UI.getCurrent())
									.cancelar();

						}
					});

				}
				
				
			}
		});
		
		return panel;
		
		
		
	}
	
	private Table CargarReservasInd(){
		
		
		Table tabla = new Table();
		
		
		IWebServiceCentralService wss = new IWebServiceCentralService();
		IWebServiceCentral ws = wss.getIWebServiceCentralPort();
		Empresa emp = (Empresa) VaadinSession.getCurrent().getAttribute("empresa");		
		List<ReservaIndependiente> listaRes = ws.listarResIndVigXEmpresa(emp);
		BeanItemContainer<ReservaIndependiente> dsReservas = new BeanItemContainer<ReservaIndependiente>(
				ReservaIndependiente.class);
		dsReservas.addNestedContainerProperty("cancha.idCancha");
		dsReservas.addAll(listaRes);
		tabla.setContainerDataSource(dsReservas);
		tabla.setSelectable(true);
		tabla.setImmediate(true);
		tabla.setSizeUndefined();
		tabla.setPageLength(listaRes.size());
		tabla.setVisibleColumns(RESERVA_VISIBLE_COLUMNS);
		tabla.setColumnHeaders(RESERVA_COLUMN_HEADERS);
		tabla.setPageLength(tabla.size());
		
		tabla.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				
				Panel panel = new Panel();
				estados.addItem("Cancelada");
				estados.addItem("Consumida");
				estados.addStyleName("vertical");
				estados.setSizeUndefined();
				estados.setImmediate(true);
				
				resSeleccionada = (ReservaIndependiente) tabla.getValue();
				VerticalLayout vLayout = new VerticalLayout();
				vLayout.addComponent(estados);
				vLayout.addComponent(btnAcepEst);
				vLayout.setMargin(true);
				vLayout.setSizeFull();
				panel.setContent(vLayout);
				sub.setContent(panel);
				sub.setModal(true);
				UI.getCurrent().addWindow(sub);
				
			}
		});
		
		
		
		return tabla;
		
	}
	
	private void panelIndependientes(){
		
		reservasInd = null;
		reservasInd = CargarReservasInd();
		
		vl.removeAllComponents();
		//vl.addComponent(tipoReserva);
		vl.setMargin(true);
		vl.setStyleName("vLayout");
		VerticalLayout vLayout = new VerticalLayout();
		vLayout.setStyleName("vLayout");
		HorizontalLayout hLayout = new HorizontalLayout();
		vLayout.addComponent(new Label("&nbsp;", ContentMode.HTML));
		vLayout.addComponent(reservasInd);
		vLayout.setComponentAlignment(reservasInd, Alignment.MIDDLE_CENTER);
		hLayout.setSpacing(true);
		hLayout.addComponent(btnSalir);
		lblAlta.setSizeUndefined();
		vl.addComponent(lblAlta);
		vl.addComponent(vLayout);
		vl.addComponent(new Label("&nbsp;", ContentMode.HTML));
		vl.addComponent(hLayout);
		vl.setComponentAlignment(lblAlta, Alignment.TOP_CENTER);
		vl.setComponentAlignment(vLayout, Alignment.MIDDLE_CENTER);
		vl.setComponentAlignment(hLayout, Alignment.BOTTOM_CENTER);
		

		panel.setContent(vl);
		
		btnSalir.addClickListener(new ClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				((PaginaPrincipal) UI.getCurrent()).cancelar();
				
			}
		});
		//return panelI;
		
	}
	
	/*private void panelFijas(){
		
		reservasFijas.addContainerProperty("Reserva", String.class,null);
		reservasFijas.addContainerProperty("Numero", Integer.class,null);
		reservasFijas.addItem(new Object[]{"reservaF1", 1},1);
		reservasFijas.addItem(new Object[]{"reservaF2",2},2);
		reservasFijas.setPageLength(3);
		reservasFijas.setSizeUndefined();
		reservasFijas.setSelectable(true);
		reservasFijas.setImmediate(true);
		vl.removeAllComponents();
		vl.addComponent(tipoReserva);
		vl.setMargin(true);
		vl.setStyleName("vLayout");
		VerticalLayout vLayout = new VerticalLayout();
		vLayout.setStyleName("vLayout");
		HorizontalLayout hLayout = new HorizontalLayout();
		vLayout.addComponent(new Label("&nbsp;", ContentMode.HTML));
		vLayout.addComponent(reservasFijas);
		vLayout.setComponentAlignment(reservasFijas, Alignment.MIDDLE_CENTER);
		hLayout.setSpacing(true);
		hLayout.addComponent(btnSalir);
		lblAlta.setSizeUndefined();
		vl.addComponent(lblAlta);
		vl.addComponent(vLayout);
		vl.addComponent(new Label("&nbsp;", ContentMode.HTML));
		vl.addComponent(hLayout);
		vl.setComponentAlignment(lblAlta, Alignment.TOP_CENTER);
		vl.setComponentAlignment(vLayout, Alignment.MIDDLE_CENTER);
		vl.setComponentAlignment(hLayout, Alignment.BOTTOM_CENTER);
		

		panel.setContent(vl);
		
		btnSalir.addClickListener(new ClickListener() {
			
			
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				((PaginaPrincipal) UI.getCurrent()).cancelar();
				
			}
		});
		
		//return panelF;
	}*/
	
	

}
